<template>
  <div class="register">
    <div class="content">
      <header>Beauty</header>
      <el-form
        ref="formRef"
        :model="formData"
        label-position="top"
        :rules="rules"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" size="default"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="formData.password"
            size="default"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
      </el-form>

      <div class="operate">
        <el-button type="danger" @click="doRegister">注册</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useApi } from '@core/http/api-instance';
import { ElForm, ElMessage } from 'element-plus';

const api = useApi();
const router = useRouter();
const formRef = ref<InstanceType<typeof ElForm>>();
const formData = reactive({
  username: '',
  password: '',
});

const rules = {
  username: [{ required: true, message: '用户名必填' }],
  password: [{ required: true, message: '密码必填' }],
};

// funcs
// 注册
const doRegister = async () => {
  if (!(await formRef.value.validate())) {
    return;
  }

  try {
    await api.login.register(formData.username, formData.password);
    ElMessage.success({
      message: '注册成功',
    });
    router.push('/login');
  } catch (e) {
    ElMessage.warning(e instanceof Error && e.message);
  }
};
</script>

<style lang="scss" scoped>
.register {
  flex: 1;

  .content {
    width: 400px;
    margin: 120px auto;
    padding: 20px 30px;
    border-radius: 5px;
    border: 1px solid var(--pink-color1);

    header {
      font-size: 30px;
      text-align: center;
      margin-bottom: 20px;
      color: var(--pink-color1);
    }
  }

  .operate {
    button {
      width: 100%;
    }
  }
}
</style>
